package com.novel.utils;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class HtmlParse {
	
	public void parse(String content){
		Parser parser;
		NodeList nodelist = null;
		List<DaoBean> list = new ArrayList<DaoBean>();
		InsertDao  dao = new InsertDao();
		try {
			parser = new Parser(content);
			parser.setEncoding("UTF-8");
			NodeFilter filter = new AndFilter(new TagNameFilter("span"),
					new TagNameFilter("span"));// 
			nodelist = parser.parse(filter);
						
			String sectionName = "";
			
			for(int i = 0; i < nodelist.size(); i++) {
				Tag tag = (Tag) nodelist.elementAt(i);
				String span = tag.getAttribute("class");
				if(span!=null&&span.equals("v")){
					sectionName = tag.toPlainTextString();
					continue;
				}else{
					Document doc = Jsoup.parse(tag.getChildren().toHtml());
					Element link = doc.select("a").first();//
					String text = doc.body().text(); // "An example link"//
					
					String linkHref = "";
					if(link!=null){
						linkHref = link.attr("href"); // "http://example.com/"//ַ
					}
					if(text!=null&&link!=null){
					//	InsertDao dao = new InsertDao();
						
						if(linkHref!=null&&sectionName!=""){
						//	String tempContent = parseConent(linkHref);
							if(!text.contains("all rights reserved")){
								DaoBean bean = new DaoBean();
								bean.setContent(linkHref);
								bean.setPageName(text);
								bean.setSectionName(sectionName);
								list.add(bean);
								
								//dao.insert(sectionName, text,tempContent);
							}
						}
					}
				}
				
				
				
			}
			
			
			if(list.size()>dao.getCount()){
				for(int i = dao.getCount(); i<list.size(); i++){
					DaoBean bean = list.get(i);
					String tempContent = parseConent(bean.getContent());
					dao.insert(bean.getSectionName(), bean.getPageName(), tempContent);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	public String parseConent(String url){
		HttpUtils utils = new HttpUtils();
		String content = null;
		try {
			content = utils.getContentFromIn(url, "utf-8");
			Parser parser;
			NodeList nodelist = null;
			
			content = content.substring(content.indexOf("<!DOCTYPE"));
		
			parser = new Parser(content);
			parser.setEncoding("utf-8");
			NodeFilter f = new HasAttributeFilter("class", "bookcontent clearfix");
			NodeFilter filter = new AndFilter(new TagNameFilter("div"),f);
			nodelist = parser.parse(filter);
			
			return nodelist.elementAt(0).toHtml();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
		
	}
	
	
	public static void main(String[] args) throws Exception {
		HttpUtils utils = new HttpUtils();
		HtmlParse parse = new HtmlParse();
		parse.parse(utils.getContentFromIn("http://www.zanghaihua.org", "Utf-8"));
	//	parse.parseConent("http://www.zanghaihua.org/2425.html");
	}
}
